package com.ttg.web.task;

import com.ttg.common.exception.BaseException;
import com.ttg.common.utils.JsonUtils;
import com.ttg.model.dto.GenerateFilesDTO;
import com.ttg.model.dto.TaskGetBankInfoDTO;
import com.ttg.web.service.TzgService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobContext;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 生成zip{.unl,.unl}生成数据压缩包
 */
@Slf4j
@Component
public class GenerateFilesByTblValueHandler {

    @Autowired
    private TzgService tzgService;


    @XxlJob("GenerateFilesByTblValueHandler")
    @Async
    public ReturnT<String> execute() {
        String param = XxlJobHelper.getJobParam();
        log.info("【GenerateFilesTask generateFilesByTblValueHandler 】获取银行信息数据 start=>{}",param);
        try {
            GenerateFilesDTO reqDto = new GenerateFilesDTO();
            if (StringUtils.isNotBlank(param)) {
                reqDto = JsonUtils.jsonToObject(param, GenerateFilesDTO.class);
            }
            tzgService.generateFilesByTblValueHandler(reqDto);
        } catch (Exception e) {
            log.error("异常调度异常：{}",e.getMessage());
            XxlJobContext.getXxlJobContext().setHandleCode(ReturnT.FAIL_CODE);
            XxlJobContext.getXxlJobContext().setHandleMsg("执行异常，请联系管理员查询相关日志" + e.getMessage());
            throw new BaseException("异常调度异常",e.getLocalizedMessage());
        }
        log.info("【MerchantJobTask getBankInfoByUnl 】获取银行商户信息数据 end");
        return ReturnT.SUCCESS;
    }

}
